package flatlandRPG;

import org.lwjgl.util.Color;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.opengl.GL11;

// -------------------------------------------------------------------------
/**
 * A 2D polygon.
 *
 * @author evan
 * @version Apr 15, 2011
 */
public class GLPolygon
    extends GLPrimitive
{
    /**
     * The points that make up the verticies of the polygon.
     */
    protected Vector2f[] points;


    // ----------------------------------------------------------
    /**
     * Create a new GLPolygon object with the given points in a clockwise
     * direction.
     *
     * @param points
     *            the points that define the vertices of the polygon.
     */
    public GLPolygon(Vector2f... points)
    {
        this.points = points;
    }


    // ----------------------------------------------------------
    /**
     * Create a new GLPolygon object from a list of floats. The points are
     * defined by alternating x and y values in the list. There must be an even
     * number of arguments.
     *
     * @param points
     *            a list of floats that defines the points
     */
    public GLPolygon(Float... points)
    {
        // There must be an even number of points
        assert points.length % 2 == 0;

        this.points = new Vector2f[points.length / 2];
        for (int i = 0; i < points.length - 1; i += 2)
        {
            this.points[i / 2] = new Vector2f(points[i], points[i + 1]);
        }

        setColor(new Color(207, 166, 101));
    }


    @Override
    protected void drawObject()
    {
        GL11.glBegin(GL11.GL_POLYGON);
        for (Vector2f p : points)
        {
            GL11.glVertex3f(p.getX(), p.getY(), 0);
        }
        GL11.glEnd();

        GL11.glBegin(GL11.GL_POLYGON);
        for (Vector2f p : points)
        {
            GL11.glVertex3f(p.getX(), p.getY(), THICKNESS);
        }
        GL11.glEnd();

        GL11.glBegin(GL11.GL_QUAD_STRIP);
        for (Vector2f p : points)
        {
            GL11.glVertex3f(p.getX(), p.getY(), 0);
            GL11.glVertex3f(p.getX(), p.getY(), THICKNESS);
        }
        GL11.glEnd();
    }
}
